<?php
namespace app\admin\controller;
use app\model\WxUactivity as WxUactivityModel;
use think\Db;


class WxUactivity extends Base
{
    public function __construct(){
        $model = new WxUactivityModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '活动';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }


    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'name','name'=>'名称','type'=>'text'],
                ['key'=>'status','name'=>'状态','type'=>'select1_1','params'=>$model->getStatusList()],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'status','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        if (isSearchParam('name')) {
            $ids = Db::name('wx_activity')->where('name','like','%'.input('name').'%')->column('id');
            $map[] = ['wx_activity_id','in',$ids];
        }

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['user'] = Db::name('user')->field('nickname,mobile')->where('id',$vo['user_id'])->find();
            $list['data'][$k]['activity'] = Db::name('wx_activity')->field('name')->where('id',$vo['wx_activity_id'])->find();
            $list['data'][$k]['fields'] = json_decode($vo['fields'],true);
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','activity_category_id'],
                'nomust'=>['image','images','description','address','activity_date','apply_date','apply_time','num','price','content','sort','type','view_num','like_num','tags']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','activity_category_id'],
                'nomust'=>['image','images','description','address','activity_date','apply_date','apply_time','num','price','content','sort','type','view_num','like_num','tags']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::updateData($id,$save_data);
    }

    public function export(){
        $model = $this->model;
        $map_params = [
            ['key'=>'status','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        if (isSearchParam('name')) {
            $ids = Db::name('wx_activity')->where('name','like','%'.input('name').'%')->column('id');
            $map[] = ['wx_activity_id','in',$ids];
        }

        $list = $model->where($map)->select();
        foreach ($list as $k => $vo) {
            $list[$k]['user'] = Db::name('user')->field('nickname,mobile')->where('id',$vo['user_id'])->find();
            $list[$k]['activity'] = Db::name('wx_activity')->field('name')->where('id',$vo['wx_activity_id'])->find();
            $list[$k]['fields'] = json_decode($vo['fields'],true);
        }
        $data = [];
        $title = ['活动名称','用户名称','用户电话','状态','报名时间','表单信息'];
        foreach ($list as $k => $vo) {
            $fields = [];
            foreach ($vo['fields'] as $field) {
                $fields[] = $field['name'].'：'.$field['value'];
            }
            $tmp = [
                $vo['activity']['name'],
                $vo['user']['nickname'],$vo['user']['mobile'],
                $model->getStatusCn($vo['status']),
                timeDef($vo['ctime']),
                implode('；',$fields)
            ];
            $data[] = $tmp;
        }
        exportCsv(date('YmdHis'),$title,$data);
    }


   
}
